package net.zoneland.knowledge.dao;

import net.zoneland.knowledge.model.PartyGroup;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * 党小组织数据接口.
 * @author zx
 */
@Mapper
public interface PartyGroupMapper {

    /**
     * 根据组织id查询组织.
     * @param orgGroupUUID 组织id
     * @return 党组织集合
     */
    @Select({
        "select ORG_GROUP_ID, ORG_GROUP_LEADER, ORG_GROUP_MEMBERS, ORG_GROUP_UUID, ORG_PARENT_UUID,",
        "a.COMPANY_DN, a.COMPANY_NAME, ORG_GROUP_NAME, b.ORG_SHORT_NAME",
        "FROM AITK_PARTY_GROUP a left join AITK_PARTY_ORG b on a.ORG_PARENT_UUID = b.ORG_UUID ",
        "WHERE ORG_GROUP_UUID = #{orgGroupUUID, jdbcType=VARCHAR}",
    })
    @Results(id = "orgResultMap", value = {
            @Result(column="ORG_GROUP_ID", property="orgGroupID", jdbcType= JdbcType.VARCHAR, id=true),
            @Result(column="ORG_GROUP_LEADER", property="orgGroupLeader", jdbcType=JdbcType.VARCHAR),
            @Result(column="ORG_GROUP_MEMBERS", property="orgGroupMembers", jdbcType=JdbcType.VARCHAR),
            @Result(column="ORG_GROUP_UUID", property="orgGroupUUID", jdbcType=JdbcType.VARCHAR),
            @Result(column="ORG_PARENT_UUID", property="orgParentUUID", jdbcType=JdbcType.VARCHAR),
            @Result(column="COMPANY_DN", property="companyDn", jdbcType=JdbcType.VARCHAR),
            @Result(column="COMPANY_NAME", property="companyName", jdbcType=JdbcType.VARCHAR),
            @Result(column="ORG_GROUP_NAME", property="orgGroupName", jdbcType=JdbcType.VARCHAR),
            @Result(column="ORG_SHORT_NAME", property="orgGroupParentName", jdbcType=JdbcType.VARCHAR)
    })
    PartyGroup getOrgByUuid(String orgGroupUUID);

    /**
     * 查询支部下的党小组uuid列表
     * @param orgUuids 支部uuid列表(存在大小写敏感问题，需做转换处理)
     * @return 党小组uuid列表
     */
    @Select({
            "<script>",
            "SELECT ORG_GROUP_UUID FROM AITK_PARTY_GROUP where UPPER(ORG_PARENT_UUID) in ",
            "<foreach collection='orgUuids' index='index' item='item' open='(' separator=',' close=')'>",
            " UPPER(#{item}) </foreach> ",
            "</script>",
    })
    List<String> getOrgGroupUuidByParentIds(@Param("orgUuids") final List<String> orgUuids);

}
